package com.kuang.service.user;

import com.kuang.dao.BaseDao;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    //    业务层调用Dao层 引入Dao曾
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    public User login(String userCode, String password) {

        System.out.println("UserServlet"+password);
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(userCode, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    public boolean updatePwd(int id, String pwd) {
        Connection connection = null;
        boolean flag = false;

//        修改密码
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection, id, pwd) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }


    public int getUserCount(String userName, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, userName, userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null, null);
        }
        return count;
    }

    /**
     * @description: 根据条件查询用户列表
     * @date 20/12/2 17:58
     * @Param: [queryUserName, queryUserRole, currentPageNo, pageSize]
     * @Return: java.util.List<com.threepure.pojo.User>
     */

    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;

        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null, null );
        }
        return userList;
    }

    public boolean add(User user) {
        Connection connection=null;
        boolean flag=false;

        try {
            connection=BaseDao.getConnection();
            //关闭自动提交，默认开启jdbc事物
            connection.setAutoCommit(false);
            int addRows = userDao.add(connection, user);
            connection.commit();
            if (addRows>0) {
                flag=true;
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }


    /**
     * @param userCode
     * @description: 根据UserCode查询用户
     * @date 20/12/7 18:03
     * @Param: [userCode]
     * @Return: com.threepure.pojo.User
     */
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    public boolean userModify(User user) {
        Connection connection=null;
        boolean flag=false;

        try {
            connection = BaseDao.getConnection();
            //关闭自动提交，默认开启jdbc事物
            connection.setAutoCommit(false);
            int i = userDao.userModify(connection, user);
            connection.commit();

            if (i>0) {
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return flag;
    }

    /**
     * @param id
     * @description: 根据id查找User
     * @date 20/12/7 18:04
     * @Param: [id]
     * @Return: com.threepure.pojo.User
     */
    public User getUserById(String id) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection, id);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    public boolean deleteUserById(Integer id) {
        Connection connection=null;
        boolean flag=false;
        try {
            connection  = BaseDao.getConnection();

            int i = userDao.deleteUserById(connection, id);
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        return flag;

    }


    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
//        int userCount = userService.getUserCount(null, 0);
//
//        System.out.println(userCount);
        User user = new User();
        user.setId(13);
        user.setUserName("高胜亚");
        user.setUserPassword("11111111");
        boolean add = userService.add(user);
        System.out.println(add);

        User user1 = userService.selectUserCodeExist("admin");
        System.out.println(user1.getUserCode());

    }
}
